package com.sky.es.util;

import java.io.Serializable;
import java.text.MessageFormat;

public class BaseResDto<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    public final static String SUCCESS = "0000";

    /**返回码 0000 为成功, 其他为失败*/
    protected String resultCode;
    /**返回描述*/
    protected String resultMsg;

    protected T bodyData;

    public BaseResDto() {
    }

    public BaseResDto(T bodyData) {
        this.bodyData = bodyData;
    }

    public static <T> BaseResDto<T> me() {
        BaseResDto<T> baseResDto = new BaseResDto<>();
        return baseResDto;
    }

    public boolean isSuccess() {
        if (SUCCESS.equalsIgnoreCase(this.resultCode)) {
            return true;
        } else {
            return false;
        }
    }

    public BaseResDto<T> success() {
        this.resultCode = SUCCESS;
        return this;
    }

    public BaseResDto<T> success(String msg) {
        this.resultCode = SUCCESS;
        this.resultMsg = msg;
        return this;
    }

    public BaseResDto<T> success(T data) {
        this.resultCode = SUCCESS;
        this.bodyData = data;
        return this;
    }

    public BaseResDto<T> failure(String msg) {
        this.resultCode = "--";
        this.resultMsg = msg;
        return this;
    }

    public BaseResDto<T> failure(String code, String msg) {
        this.resultCode = code;
        this.resultMsg = msg;
        return this;
    }

    public BaseResDto<T> failure(String msg, int... args) {
        this.resultCode = "--";
        this.resultMsg = MessageFormat.format(msg, args);
        return this;
    }
    public BaseResDto<T> failure(String msg, long... args) {
        this.resultCode = "--";
        this.resultMsg = MessageFormat.format(msg, args);
        return this;
    }
    public BaseResDto<T> failure(String msg, double... args) {
        this.resultCode = "--";
        this.resultMsg = MessageFormat.format(msg, args);
        return this;
    }

    public BaseResDto<T> failure(String code, String msg, Object... args) {
        this.resultCode = code;
        this.resultMsg = MessageFormat.format(msg, args);
        return this;
    }

    public BaseResDto<T> setBodyData(T bodyData) {
        this.bodyData = bodyData;
        return this;
    }

    public T getBodyData() {
        return bodyData;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }
}
